export const ACTION_SET_FROM = 'ACTION_SET_FROM';
export const ACTION_SET_TO = 'ACTION_SET_TO';
//城市选择浮层是否可见
export const ACTION_SET_ISCITY_SELECT_VISIBLE =
  'ACTION_SET_ISCITY_SELECT_VISIBLE';
//export const ACTION_SET_//城市选择后返="城市选择后返",初始值用false来替
export const ACTION_SET_CURRENT_SELECT_LEFT_CITY =
  'ACTION_SET_CURRENT_SELECT_LEFT_CITY';
//异步加载的城市列表数据
export const ACTION_SET_CITYDATA = 'ACTION_SET_CITYDATA';
export const ACTION_SET_ISLOADING_CITY_DATA = 'ACTION_SET_ISLOADING_CITY_DATA';
//是否显示 日期选择浮层
export const ACTION_SET_ISDATE_SELECT_VISIBLE =
  'ACTION_SET_ISDATE_SELECT_VISIBLE';
//是否选择高铁动车
export const ACTION_SET_HIGHSPEED = 'ACTION_SET_HIGHSPEED';
//乘车日期
export const ACTION_SET_DEPARTDATE = 'ACTION_SET_DEPARTDATE';
//设置始发站
export function setFrom(from) {
  return {
    type: ACTION_SET_FROM,
    payload: from,
  };
}
//设置终到站
export function setTo(to) {
  return {
    type: ACTION_SET_TO,
    payload: to,
  };
}
//设置加载 城市列表状态
export function setIsLoadingCityData(isLoadingCityData) {
  return {
    type: ACTION_SET_ISLOADING_CITY_DATA,
    payload: isLoadingCityData,
  };
}
// 设置当前城市
export function setCityData(cityData) {
  return {
    type: ACTION_SET_CITYDATA,
    payload: cityData,
  };
}
/**
 * 使用了redux-thunk 可以接受异步action
 */
export function toggleHighSpeed() {
  return (dispatch, getState) => {
    const { highSpeed } = getState();
    dispatch({
      type: ACTION_SET_HIGHSPEED,
      payload: !highSpeed,
    });
  };
}
//显示城市浮层
export function showCitySelect(currentSelectCity) {
  return (dispatch) => {
    //设置城市列表浮层可见
    dispatch({
      type: ACTION_SET_ISCITY_SELECT_VISIBLE,
      payload: true,
    });
    //设置当前点击的是左边还是右边的站点  currentSelectCity :true 左边;false 右边
    dispatch({
      type: ACTION_SET_CURRENT_SELECT_LEFT_CITY,
      payload: currentSelectCity,
    });
  };
}

//隐藏城市浮层  快捷键efn
export const hideCitySelect = () => {
  return {
    type: ACTION_SET_ISCITY_SELECT_VISIBLE,
    payload: false,
  };
};

//设置当前城市  根据ACTION_SET_CURRENT_SELECT_LEFT_CITY的值去填入始发站还是终点站
export const setSelectedCity = (currentCity) => {
  return (dispatch, getState) => {
    //判断是始发站还是终点站逻辑
    const { currentSelectLeftCity } = getState();
    if (currentSelectLeftCity) {
      dispatch(setFrom(currentCity));
    } else {
      dispatch(setTo(currentCity));
    }
    //隐藏城市浮层
    dispatch(hideCitySelect());
  };
};
//显示日期浮层
export const showDateSelect = () => {
  return {
    type: ACTION_SET_ISDATE_SELECT_VISIBLE,
    payload: true,
  };
};
//隐藏日期浮层
export const hideDateSelect = () => {
  return {
    type: ACTION_SET_ISDATE_SELECT_VISIBLE,
    payload: false,
  };
};
//切换始发终点站
export const exchangeFromTo = () => {
  return (dispatch, getState) => {
    const { from, to } = getState();
    dispatch(setFrom(to));
    dispatch(setTo(from));
  };
};
//切换乘车日期
export const setDepartDate = (departDate) => {
  return { type: ACTION_SET_DEPARTDATE, payload: departDate };
};

export const fetchCityData = () => {
  return (dispatch, getState) => {
    const { isLoadingCityData } = getState();
    if (isLoadingCityData) return;

    if (localStorage) {
      let cache = JSON.parse(localStorage.getItem('_$local_cache$_') || '{}');

      if (cache.expire > Date.now()) {
        //数据未过时,从缓存中获取
        // console.log('缓存数据未过时:' + JSON.stringify(cache))
        dispatch(setCityData(cache.data));
        return;
      }
    }
    console.log('setLoading:true');
    //设置加载状态
    dispatch(setIsLoadingCityData(true));

    //发起请求

    fetch('/rest/cities?_' + Date.now())
      .then((res) => res.json())
      .then((cityData) => {
        dispatch(setCityData(cityData));
        if (localStorage) {
          //设置缓存
          localStorage.setItem(
            '_$local_cache$_',
            JSON.stringify({
              data: cityData,
              expire: Date.now() + 60 * 1000,
            })
          );
        }
        console.log(`获取到cities数据:${cityData}`);
        dispatch(setIsLoadingCityData(false));
      })
      .catch(() => {
        //获取不到数据 ,导致设置为false 会在副作用哪里触发依赖改变 重新发起请求,造成死循环

        console.log('获取不到cities 数据');
        dispatch(setCityData({}));
        dispatch(setIsLoadingCityData(false));
      });
  };
};
